package com.timemanage.activity.ui.notifications

import android.content.Context
import android.content.Intent
import android.os.Build
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.RecyclerView
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.animator.PopupAnimator
import com.lxj.xpopup.core.CenterPopupView
import com.timemanage.R
import com.timemanage.dao.AppDatabase
import com.timemanage.dao.UserDao
import com.timemanage.entity.User
import com.timemanage.entity.Work
import com.timemanage.utils.MyApplication
import kotlinx.android.synthetic.main.todo_finish.view.*
import kotlinx.android.synthetic.main.work_add.view.*
import kotlinx.android.synthetic.main.work_detail.view.*
import kotlinx.android.synthetic.main.work_detail.view.work_body
import kotlinx.android.synthetic.main.work_detail.view.work_title
import java.text.SimpleDateFormat
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util.*
import kotlin.collections.ArrayList
import kotlin.concurrent.thread

class todoAdapter(val context: Context,val sort_id:ArrayList<String>,val sort_name:ArrayList<String>,
                    val sort_body:ArrayList<String>,val start_Time:ArrayList<String>,
                    val end_Time:ArrayList<String>):RecyclerView.Adapter<todoAdapter.MyViewHolder>() {
    inner class MyViewHolder(view:View):RecyclerView.ViewHolder(view){
        val sortName: TextView = itemView.findViewById(R.id.sort_name_text)
        val sortBody: TextView = itemView.findViewById(R.id.sort_body_text)
        val startTime : TextView = itemView.findViewById(R.id.starttime_text)
        val endTime : TextView = itemView.findViewById(R.id.endtime_text)
        val mainLayout: LinearLayout = itemView.findViewById(R.id.mainLayout)
    }
    var work_Id = ""
    var work_Name = ""
    var work_Body = ""
    var work_Start = ""
    var work_End = ""
    var work_Start2Cpm :Long= 0
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(
            R.layout.row_item1,parent,false
        )
        return MyViewHolder(view)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val sdf = SimpleDateFormat("yyyy/MM/dd hh:mm a")
        //"yyyy/MM/dd hh:mm a"
        val Datestart = sdf.format(Date(start_Time[position].toLong()))
        val Dateend = sdf.format(Date(end_Time[position].toLong()))
        work_Name = sort_name[position]
        work_Body = sort_body[position]
        work_Start = Datestart
        work_End = Dateend
        work_Start2Cpm = start_Time[position].toLong()
        work_Id = sort_id[position]
        holder.sortName.text = sort_name[position]
        holder.sortBody.text = sort_body[position]
        holder.startTime.text = Datestart
        holder.endTime.text = Dateend
        holder.mainLayout.setOnClickListener {
            XPopup.Builder(MyApplication.context)
                .asCustom(todoFishi(context))
                .show();
        }
    }

    override fun getItemCount() = sort_name.size
    inner class todoFishi(context: Context): CenterPopupView(context){
        override fun getImplLayoutId(): Int {
            return R.layout.todo_finish
        }
        @RequiresApi(Build.VERSION_CODES.O)
        override fun onCreate(){
            super.onCreate()
            work_title1.text = work_Name
            work_body1.text = work_Body
            startTime1.text = work_Start
            endTime1.text = work_End
            floatingActionButtoncommit1.setOnClickListener {
                var database = AppDatabase.getDatabase(MyApplication.context)
                var userdao: UserDao = database.userDao();
                var workDao = database.WorkDao()
                val userid = userdao.selectAcUser().userId
                // LocalDate.parse("14-02-2018", DateTimeFormatter.ofPattern("dd-MM-yyyy"))
                //val nowStamp =LocalDate.parse(work_Start, DateTimeFormatter.ofPattern("yyyy/MM/dd hh:mm a"))
                if(System.currentTimeMillis()>work_Start2Cpm) {
                    workDao.updateEndTime(work_Id.toInt(), System.currentTimeMillis())
                    dismiss()
                }else{
                    Toast.makeText(context,"待办事项尚未开始，不能提前结束",Toast.LENGTH_SHORT).show()
                }
            }
        }
    }
}